package com.pricy.me.model;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.atermenji.android.iconictextview.icon.EntypoIcon;
import com.atermenji.android.iconictextview.icon.FontAwesomeIcon;
import com.atermenji.android.iconictextview.icon.Icon;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class CategoryIcon extends Category{
	private Icon icon;
	private boolean checked;
	
	public CategoryIcon(int id, String title, Icon icon) {
		super(id, title);
		this.icon = icon;
	}
	public CategoryIcon(int id) {
		super(id);
	}
	
	public List<Category> getAll(){
		List<Category> c = new ArrayList<Category>();
		c.add(new Category(7, "Art"));
		c.add(new Category(10, "Auto & Moto"));
		c.add(new Category(6, "Cadeaux & Fêtes"));
		c.add(new Category(5, "Décoration intérieure"));
		c.add(new Category(11, "Education"));
		c.add(new Category(12, "Enfants"));
		c.add(new Category(4, "Mariage"));
		c.add(new Category(3, "Mode femmes"));
		c.add(new Category(2, "Mode hommes"));
		c.add(new Category(8, "Restauration & café"));
		c.add(new Category(9, "Sport"));
		c.add(new Category(1, "Technologie"));
		return c;
	}
	@SuppressWarnings("unchecked")
	public List<CategoryIcon> getAll(Context context){
		List<CategoryIcon> c = new ArrayList<CategoryIcon>();
		c.add(new CategoryIcon(7, "Art", EntypoIcon.PALETTE));
		c.add(new CategoryIcon(10, "Auto & Moto", FontAwesomeIcon.DASHBOARD));

		c.add(new CategoryIcon(6, "Cadeaux & Fêtes", FontAwesomeIcon.GIFT));
		c.add(new CategoryIcon(5, "Décoration intérieure", FontAwesomeIcon.HOME));

		c.add(new CategoryIcon(11, "Education", EntypoIcon.GRADUATION_CAP));
		c.add(new CategoryIcon(12, "Enfants", FontAwesomeIcon.SMILE));
		c.add(new CategoryIcon(4, "Mariage", FontAwesomeIcon.HEART));
		c.add(new CategoryIcon(3, "Mode femmes", FontAwesomeIcon.FEMALE));
		c.add(new CategoryIcon(2, "Mode hommes", FontAwesomeIcon.MALE));

		c.add(new CategoryIcon(8, "Restauration & café", FontAwesomeIcon.FOOD));

		c.add(new CategoryIcon(9, "Sport", FontAwesomeIcon.TROPHY));
		c.add(new CategoryIcon(1, "Technologie", FontAwesomeIcon.DESKTOP));
		
		Gson gson = new Gson();
    	String jsonOutput = readFile("usercategory.txt", context);
    	
    	List<Category> category = new ArrayList<Category>();
    	if(jsonOutput.length() > 2){
    		Type listType = new TypeToken<ArrayList<Category>>(){}.getType();
    		category = (ArrayList<Category>) gson.fromJson(jsonOutput, listType);
    		for (CategoryIcon cat : c) {
    			for (Category catloc : category) {
    				if(cat.getId() == catloc.getId()) cat.setChecked(true);
    			}
			}
    	}
		return c;
	}
	
	public CharSequence[] getAllChar(){

		CharSequence[] c = {"Art", "Auto & Moto", "Cadeaux & Fêtes","Décoration intérieure", "Education", "Enfants", "Mariage", 
				"Mode femmes", "Mode hommes", "Restauration & café", "Sport", "Technologie"};

		return c;
	}
	
	@SuppressWarnings("unchecked")
	public boolean[] getAllChecked(Context context){
		
		List<CategoryIcon> c = new ArrayList<CategoryIcon>();
		c.add(new CategoryIcon(7, "Art", EntypoIcon.PALETTE));
		c.add(new CategoryIcon(10, "Auto & Moto", FontAwesomeIcon.DASHBOARD));

		c.add(new CategoryIcon(6, "Cadeaux & Fêtes", FontAwesomeIcon.GIFT));
		c.add(new CategoryIcon(5, "Décoration intérieure", FontAwesomeIcon.HOME));

		c.add(new CategoryIcon(11, "Education", EntypoIcon.GRADUATION_CAP));
		c.add(new CategoryIcon(12, "Enfants", FontAwesomeIcon.SMILE));
		c.add(new CategoryIcon(4, "Mariage", FontAwesomeIcon.HEART));
		c.add(new CategoryIcon(3, "Mode femmes", FontAwesomeIcon.FEMALE));
		c.add(new CategoryIcon(2, "Mode hommes", FontAwesomeIcon.MALE));

		c.add(new CategoryIcon(8, "Restauration & café", FontAwesomeIcon.FOOD));

		c.add(new CategoryIcon(9, "Sport", FontAwesomeIcon.TROPHY));
		c.add(new CategoryIcon(1, "Technologie", FontAwesomeIcon.DESKTOP));
		boolean[] b = new boolean[c.size()];
		
		Gson gson = new Gson();
    	String jsonOutput = readFile("usercategory.txt", context);
    	
    	List<Category> category = new ArrayList<Category>();
    	if(jsonOutput.length() > 2){
    		Type listType = new TypeToken<ArrayList<Category>>(){}.getType();
    		category = (ArrayList<Category>) gson.fromJson(jsonOutput, listType);
    		
    		for (int i = 0; i < c.size(); i++) {
    			b[i] = false;
				for (int j = 0; j < category.size(); j++) {
					if(category.get(j).getId() == c.get(i).getId()) b[i] = true; 
				}
			}
    	}
		return b;
	}
	
	public Icon getIcon() {
		return icon;
	}
	public void setIcon(Icon icon) {
		this.icon = icon;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	
	public void toggleChecked()
    {
        checked = !checked;
    }

	private String readFile(String file, Context c) {
		StringBuilder json = new StringBuilder();
		try {
			FileInputStream fis = c.openFileInput(file);
			BufferedReader r = new BufferedReader(new InputStreamReader(fis));
			String line;
			while ((line = r.readLine()) != null) {
			    json.append(line);
			}
			fis.close();
			return json.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return "0";
		} catch (IOException e) {
			e.printStackTrace();
			return "0";
		}
	}
	
}
